package com.ruoyi.business.controller;

import com.ruoyi.business.service.ITRegisterBaseService;
import com.ruoyi.business.service.ITRegisterInfoService;
import com.ruoyi.business.service.ITRegisterNoticeService;
import com.ruoyi.business.vo.*;
import com.ruoyi.business.vo.export.RegisterManualAllInfoVo;
import com.ruoyi.business.vo.export.RegisterManualInfoVo;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RateLimiter;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;


/**
 * 管理端
 *
 * @author Tellsea
 * @date 2024/1/26
 */
@Api(tags = "管理端")
@RestController
@RequestMapping("/business/registerBase")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class TRegisterBaseController {

    private final ITRegisterBaseService registerBaseService;

    private final ITRegisterInfoService registerInfoService;

    private final ITRegisterNoticeService registerNoticeService;


    @ApiOperation("统计查询汇总")
    @GetMapping("/queryByCount")
    public AjaxResult queryByCount(TRegisterBaseVo entity) {
        entity.setUserId(SecurityUtils.getLoginUser().getUserId());
        return AjaxResult.success(registerBaseService.queryByCount(entity));
    }

    @ApiOperation("统计查询列表")
    @GetMapping("/queryByDept")
    public TableDataInfo<TRegisterBaseVo> queryByDept(TRegisterBaseVo entity) {
        entity.setUserId(SecurityUtils.getLoginUser().getUserId());
        return registerBaseService.queryByDept(entity);
    }

    @ApiOperation("批量审核")
    @PostMapping("/saveAudit")
    @RateLimiter
    public AjaxResult saveAudit(@RequestBody TAuditLogVo entity) {
        return registerBaseService.saveAudit(entity);
    }

    @ApiOperation("批量通知")
    @PostMapping("/saveNotice")
    @RateLimiter
    public AjaxResult saveNotice(@RequestBody TRegisterNoticeVo entity) {
        try{
            registerBaseService.saveNotice(entity);
        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }

        return AjaxResult.success();
    }

    @ApiOperation("批量通知预判断")
    @PostMapping("/judgeNotice")
    @RateLimiter
    public AjaxResult judgeNotice(@RequestBody TRegisterNoticeVo entity) {
        try{
            registerBaseService.judgeNoticeSend(entity);
        }catch (Exception e){
           return AjaxResult.error(e.getMessage());
        }

        return AjaxResult.success();
    }

    @ApiOperation("设置生源类型")
    @PostMapping("/saveSource")
    @RateLimiter
    public AjaxResult saveSource(@RequestBody TRegisterInfoVo entity) {
        return registerBaseService.saveSource(entity);
    }

    @ApiOperation("修改通知状态")
    @PostMapping("/updateNotice")
    @RateLimiter
    public AjaxResult updateNotice(@RequestBody TRegisterInfoVo entity) {
        return registerBaseService.updateNotice(entity);
    }

    @ApiOperation("导出工牌")
    @GetMapping("/exportBadge")
    public void exportBadge(@RequestParam("ids") Long[] ids) {
        registerBaseService.exportBadge(ids);
    }

    @ApiOperation("导出工牌根据条件筛选")
    @PostMapping("/exportFilterBadge")
    public void exportFilterBadge(HttpServletResponse response,TRegisterInfoVo entity) {
        entity.setUserId(SecurityUtils.getLoginUser().getUserId());
        List<TRegisterInfoVo> list = registerInfoService.queryAllSingle(entity);
        registerBaseService.exportBadgeSingle(list, response);
    }

    @ApiOperation("报到二维码")
    @GetMapping("/genQrCode")
    public void genQrCode() {
        registerBaseService.genQrCode();
    }

    @ApiOperation("分配带教")
    @PostMapping("/saveAllocation")
    @RateLimiter
    public AjaxResult saveAllocation(@RequestBody TRegisterAllocationVo entity) {
        return registerBaseService.saveAllocation(entity);
    }

    @ApiOperation("变更进修")
    @PostMapping("/saveModify")
    @RateLimiter
    public AjaxResult saveModify(@RequestBody TRegisterNoticeVo entity) {
        return registerBaseService.saveModify(entity);
    }

    @ApiOperation("导出培养考核-考评分数")
    @PostMapping("/scoreExport")
    @RateLimiter
    public void scoreExport(TRegisterInfoVo entity) {
        registerBaseService.scoreExport(entity);
    }

    @ApiOperation("导入培养考核-考评分数")
    @PostMapping("/scoreImport")
    @RateLimiter
    public AjaxResult scoreImport(MultipartFile file) {
        return registerBaseService.scoreImport(file);
    }

    @ApiOperation("导出培养考核-考评等级")
    @PostMapping("/levelExport")
    @RateLimiter
    public void levelExport(TRegisterInfoVo entity) {
        registerBaseService.levelExport(entity);
    }

    @ApiOperation("导入培养考核-考评等级")
    @PostMapping("/levelImport")
    @RateLimiter
    public void levelImport(MultipartFile file) {
        registerBaseService.levelImport(file);
    }

    @ApiOperation("导出学员手册")
    @GetMapping("/exportManual")
    @RateLimiter
    public void exportManual(@RequestParam("ids") Long[] ids) {
        registerBaseService.exportManual(ids);
    }

    @ApiOperation("导出学员手册")
    @GetMapping("/exportManualPdf")
    @RateLimiter
    public void exportManualPdf(@RequestParam("id") Long id) {
        registerBaseService.exportManualPdf(id);
    }

    @ApiOperation("导出学员")
//    @Log(title = "导出学员", businessType = BusinessType.EXPORT)
    @PostMapping("/exportRegisterInfo")
//    @PreAuthorize("@ss.hasPermi('business:registerBase:exportRegisterInfo')")
    @RateLimiter
    public void exportRegisterInfo(TRegisterInfoVo tRegisterInfoVo) {
        tRegisterInfoVo.setUserId(SecurityUtils.getLoginUser().getUserId());
        List<RegisterManualInfoVo> list = registerInfoService.exportRegisterInfo(tRegisterInfoVo);
        ExcelUtil<RegisterManualInfoVo> util = new ExcelUtil<>(RegisterManualInfoVo.class);
        util.exportExcel(ServletUtils.getResponse(), list, "导出学员");
    }

    @ApiOperation("导出学员汇总")
    @Log(title = "导出学员汇总", businessType = BusinessType.EXPORT)
    @PostMapping("/exportRegisterInfoAll")
    @RateLimiter
    public void exportRegisterInfoAll(TRegisterInfoVo tRegisterInfoVo) {
        tRegisterInfoVo.setUserId(SecurityUtils.getLoginUser().getUserId());
        List<RegisterManualAllInfoVo> list = registerInfoService.exportRegisterInfoAll(tRegisterInfoVo);
        ExcelUtil<RegisterManualAllInfoVo> util = new ExcelUtil<>(RegisterManualAllInfoVo.class);
        util.exportExcel(ServletUtils.getResponse(), list, "导出学员汇总");
    }

    @ApiOperation("考评评分")
    @PostMapping("/saveScoreRow")
    @RateLimiter
    public AjaxResult saveScoreRow(@RequestBody TRegisterAllocationVo entity) {
        return registerBaseService.saveScoreRow(entity);
    }

    @ApiOperation("考勤状态调整")
    @PostMapping("/saveLeave")
    @RateLimiter
    public AjaxResult saveLeave(@RequestBody TRegisterLeaveVo entity) {
        return registerBaseService.saveLeave(entity);
    }

    @ApiOperation("批量考勤状态调整")
    @PostMapping("/batchSaveLeave")
    @RateLimiter
    public AjaxResult batchSaveLeave(@RequestBody TRegisterLeaveAddVo entity) {
        return registerBaseService.batchSaveLeave(entity);
    }

    @ApiOperation("一键销假")
    @PostMapping("/saveLoseLeave")
    @RateLimiter
    public AjaxResult saveLoseLeave(@RequestBody TRegisterLeaveVo entity) {
        AjaxResult ajaxResult = registerBaseService.saveLoseLeave(entity);
        registerNoticeService.overLeaveDurationHandle();
        registerNoticeService.revertOverLeaveDurationHandle();
        return ajaxResult;
    }

    @ApiOperation("一键销假")
    @PostMapping("/batchSaveLoseLeave")
    @RateLimiter
    public AjaxResult batchSaveLoseLeave(@RequestBody TRegisterLeaveAddVo entity) {
        AjaxResult ajaxResult = registerBaseService.batchSaveLoseLeave(entity);
        registerNoticeService.overLeaveDurationHandle();
        registerNoticeService.revertOverLeaveDurationHandle();
        return ajaxResult;
    }

    @ApiOperation("结业审批")
    @PostMapping("/saveGraduation")
    @RateLimiter
    public AjaxResult saveGraduation(@RequestBody TAuditLogVo entity) {
        return registerBaseService.saveGraduation(entity);
    }

    @ApiOperation("发送评价通知")
    @PostMapping("/saveEvaluateNotice")
    @RateLimiter
    public AjaxResult saveEvaluateNotice(@RequestBody TMessageInfoVo entity) {
        return registerBaseService.saveEvaluateNotice(entity);
    }

    @ApiOperation("查看短期证书")
    @GetMapping("/queryByCertificate")
    @RateLimiter
    public void queryByCertificate(TRegisterInfoVo entity) {
        registerBaseService.queryByCertificate(entity);

    }

    @ApiOperation("导入报名信息模版")
    @GetMapping("/importRegisterTemplate")
    public void importRegisterTemplate() {
        registerBaseService.importRegisterTemplate();
    }

    @ApiOperation("导入报名信息")
    @PostMapping("/importRegister")
    public AjaxResult importRegister(MultipartFile file, boolean updateSupport) throws Exception {
        return registerBaseService.importRegister(file);
    }
}
